package run.halo.app.controller.content;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import run.halo.app.dto.ResultDTO;
import run.halo.app.dto.request.ToDoReqDTO;
import run.halo.app.enums.ToDoStatusEnum;
import run.halo.app.enums.ToDoTypeEnum;
import run.halo.app.model.entity.TodoList;
import run.halo.app.service.ThemeService;
import run.halo.app.service.TodoListService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "todolist")
public class TodoListController {

    @Autowired
    ThemeService themeService;

    @Resource(name = "todoListService")
    TodoListService todoListService;

    @GetMapping("/list")
    public String getTodoList(Model model) {

        List<TodoList> doingLists = todoListService.listToDosByToDoStatus(ToDoStatusEnum.DOING.getCode(), 0);
        List<TodoList> finishedLists = todoListService.listToDosByToDoStatus(ToDoStatusEnum.FINISHED.getCode(), 0);
        List<TodoList> expiredLists = todoListService.listToDosByToDoStatus(ToDoStatusEnum.EXPIRED.getCode(), 0);
//        List<TodoList> otherLists = todoListService.listToDosByToDoType(ToDoStatusEnum.DELETED.getCode(), 0);

        HashMap<String, List<TodoList>> todomap = new HashMap<>();
        todomap.put("doing", doingLists);
        todomap.put("finished", finishedLists);
        todomap.put("expired", expiredLists);

        model.addAttribute("is_todolist", true);
        model.addAttribute("todoList", todomap);
        return themeService.render("todolist");
    }

    @ResponseBody
    @PostMapping("/todo")
    public ResultDTO createToDo(@RequestBody ToDoReqDTO todo) {
        TodoList todoList = new TodoList();
        todoList.setTodoDes(todo.getDesc());
        TodoList todoList1 = todoListService.saveToDo(todoList);
        if (todoList1 != null) {
            return ResultDTO.success("新增成功", todoList1);
        }
        return ResultDTO.error("新增失败");
    }

}
